/*
 * Copyright 2010 Christoph Widulle (christoph.widulle@googlemail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
 * License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS"
 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */



package com.android1.amarena2d.engine;

import com.android1.amarena2d.annotations.Threadsave;
import com.android1.amarena2d.commons.Cleanable;

import java.util.ArrayList;

@Threadsave
public class GarbageManager extends EngineObject {

    private ArrayList<Object> binList;

    public GarbageManager() {
        this(100);
    }

    public GarbageManager(int size) {
        resize(size);
    }

    public void resize(int size) {
        binList = new ArrayList<Object>(size);
    }

    public synchronized void put(Object o) {
        binList.add(o);
    }

    public synchronized void free() {
        for (int i = 0; i < binList.size(); i++) {
            Object o = binList.get(i);
            if(o instanceof Cleanable){
                ((Cleanable) o).cleanup();
            }
        }
        binList.clear();
        System.gc();
    }
}

